package cn.cnf.controller;

import cn.cnf.common.Result;
import cn.cnf.common.enums.MoneyRecordType;
import cn.cnf.common.enums.RecommendState;
import cn.cnf.common.enums.StatusCode;
import cn.cnf.common.enums.VerifyState;
import cn.cnf.common.enums.WorkType;
import cn.cnf.common.exception.CustomException;
import cn.cnf.service.ChapterService;
import cn.cnf.service.ComicService;
import cn.cnf.service.MoneyRecordService;
import cn.cnf.service.NovelService;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * @author ZhuMeiYi
 * @date 2021/1/10 13:55
 * @description 管理员控制层
 */
@Slf4j
@RequiredArgsConstructor
@RestController
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private ChapterService chapterService;
    @Autowired
    private ComicService comicService;
    @Autowired
    private MoneyRecordService moneyRecordService;
    @Autowired
    private NovelService novelService;

    /**
     * 审核漫画
     * @param comicId
     * @param state
     * @return
     */
    @GetMapping("/comicVerify")
    public Result comicVerify(@RequestParam("comicId") String comicId, @RequestParam("state") Integer state) {
        try {
            return Result.success(comicService.comicVerify(comicId, VerifyState.getByValue(state)));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 审核漫画
     * @param novelId
     * @param state
     * @return
     */
    @GetMapping("/novelVerify")
    public Result novelVerify(@RequestParam("novelId") String novelId, @RequestParam("state") Integer state) {
        try {
            return Result.success(novelService.novelVerify(novelId, VerifyState.getByValue(state)));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    @GetMapping("/chapterVerify")
    public Result updateChapter(@RequestParam("chapterId") String chapterId, @RequestParam("state") Integer state,@RequestParam("workType")Integer workType) {
        try {
            return Result.success(chapterService.chapterVerify(chapterId,WorkType.getByValue(workType), VerifyState.getByValue(state)));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 返回全部未审核漫画
     *
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/comicVerifyList")
    public Result comicVerifyList(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {
        try {
            return Result.success(comicService.getVerifying(current, pageSize));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 返回全部未审核漫画
     *
     * @param current
     * @param pageSize
     * @return
     */
    @GetMapping("/novelVerifyList")
    public Result novelVerifyList(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize) {
        try {
            return Result.success(novelService.getVerifying(current, pageSize));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 推荐漫画
     */
    @GetMapping("/recommendComic")
    public Result recommendComic(@RequestParam("comicId") String comicId
            ,@RequestParam("recommendState")Integer recommendState) {
        try {
            return Result.success(comicService.recommend(comicId, RecommendState.getByValue(recommendState)));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 推荐小说
     */
    @GetMapping("/recommendNovel")
    public Result recommendNovel(@RequestParam("novelId") String novelId
            ,@RequestParam("recommendState")Integer recommendState) {
        try {
            return Result.success(novelService.recommend(novelId, RecommendState.getByValue(recommendState)));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 金额流水
     */
    @GetMapping("/moneyRecordList")
    public Result moneyRecordList(@RequestParam("current") Integer current, @RequestParam("pageSize") Integer pageSize, String fromName, String toName, Integer type,String time) {
        try {
            return Result.success(moneyRecordService.getRecord(current, pageSize, fromName, toName, MoneyRecordType.getByValue(type),time));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }

    /**
     * 全部未审核章节
     *
     * @param fromId
     * @return
     */
    @GetMapping("/verifyingChapter")
    public Result getChapterById(String fromId,Integer workType) {
        try {
            return Result.success(chapterService.getVerifyingChapter(fromId, WorkType.getByValue(workType)));
        } catch (CustomException e) {
            log.warn(e.getMessage());
            return Result.warn(e.getMessage());
        } catch (Exception ex) {
            log.error(ex.getMessage(), ex);
            return Result.error(StatusCode.SYSTEM_ERROR.getMessage());
        }
    }
}
